<template>
  <div class="payment-list">
    <div class="page-header">
      <h2>支付订单列表</h2>
      <el-button type="primary" @click="$router.push('/payment/create')">
        创建支付订单
      </el-button>
    </div>

    <el-form :inline="true" :model="queryForm" class="query-form">
      <el-form-item label="订单号">
        <el-input v-model="queryForm.orderNo" placeholder="请输入订单号" />
      </el-form-item>
      <el-form-item label="支付状态">
        <el-select v-model="queryForm.status" placeholder="请选择状态">
          <el-option label="全部" :value="null" />
          <el-option label="待支付" :value="0" />
          <el-option label="支付成功" :value="1" />
          <el-option label="支付失败" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式">
        <el-select v-model="queryForm.paymentType" placeholder="请选择支付方式">
          <el-option label="全部" :value="null" />
          <el-option label="支付宝" :value="1" />
          <el-option label="微信支付" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="orderNo" label="订单号" width="180" />
      <el-table-column prop="amount" label="金额" width="120">
        <template #default="{ row }">
          ¥{{ row.amount }}
        </template>
      </el-table-column>
      <el-table-column prop="paymentType" label="支付方式" width="120">
        <template #default="{ row }">
          {{ row.paymentType === 1 ? '支付宝' : '微信支付' }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button
            v-if="row.status === 0"
            type="primary"
            size="small"
            @click="handlePay(row)"
          >
            支付
          </el-button>
          <el-button
            v-if="row.status === 0"
            type="danger"
            size="small"
            @click="handleCancel(row)"
          >
            取消
          </el-button>
          <el-button
            type="info"
            size="small"
            @click="handleDetail(row)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const queryForm = ref({
  orderNo: '',
  status: null,
  paymentType: null
})

const getStatusType = (status) => {
  const types = {
    0: 'warning',
    1: 'success',
    2: 'danger'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    0: '待支付',
    1: '支付成功',
    2: '支付失败'
  }
  return texts[status] || '未知'
}

const fetchData = async () => {
  try {
    const response = await axios.get('/api/payment/query', {
      params: {
        ...queryForm.value,
        page: currentPage.value,
        size: pageSize.value
      }
    })
    tableData.value = response.data.records
    total.value = response.data.total
  } catch (error) {
    ElMessage.error('获取数据失败')
  }
}

const handleQuery = () => {
  currentPage.value = 1
  fetchData()
}

const resetQuery = () => {
  queryForm.value = {
    orderNo: '',
    status: null,
    paymentType: null
  }
  handleQuery()
}

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchData()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchData()
}

const handlePay = (row) => {
  router.push(`/payment/pay/${row.orderNo}`)
}

const handleCancel = async (row) => {
  try {
    await ElMessageBox.confirm('确认取消该支付订单吗？', '提示', {
      type: 'warning'
    })
    await axios.post(`/api/payment/cancel/${row.orderNo}`)
    ElMessage.success('取消成功')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('取消失败')
    }
  }
}

const handleDetail = (row) => {
  router.push(`/payment/detail/${row.orderNo}`)
}

// 查看订单详情
const viewDetail = (orderNo) => {
  router.push(`/payment/detail/${orderNo}`)
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.payment-list {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.query-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 